// pages/order/reserveorder/reserveorder.js
let AJAX = require("../../../utils/util.js").AJAX
const App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: '',
    productId: '',
    productData: '',
    num: 1,
    minusStatus: 'disable',
    agree: false,
    studentList: [],
    concatname: '',
    concatphone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productId:options.id,
      navH: App.globalData.navHeight

    })
    this.requestProInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('student')) {
      let _data = this.data.studentList
      let _i = JSON.parse(wx.getStorageSync('student')).index
      if (_i == 'add') {
        _data.push(JSON.parse(wx.getStorageSync('student')).list)
      } else {
        _data.splice(_i, 1, JSON.parse(wx.getStorageSync('student')).list)
      }

      this.setData({
        studentList: _data
      })
      wx.removeStorageSync('student')
    }
  },
  bindMinus: function () {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  toAddstudent() {
    wx.navigateTo({
      url: '/pages/order/studentinfo/studentinfo',
    })
  },
  /*点击加号*/
  bindPlus: function () {
    var num = this.data.num;
    num++;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  /*输入框事件*/
  bindManual: function (e) {
    var num = e.detail.value;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack(){
    wx.navigateBack()
  },
  requestProInfo(){
    let vm =this
    AJAX("product/info","GET",{
      id:vm.data.productId
    }).then(res=>{
      vm.setData({
        productData:res.data
      })
    })
  },
  agreeClick(){
    let _val = !this.data.agree
    this.setData({
      agree: _val
    })
  },
  
  submitOrder(){
    let vm = this
    if (!vm.data.agree) {
      wx.showToast({
        title: '请阅读并且同意相关条列',
        icon: "none"
      })
      return
    }
    if (!vm.data.concatname) {
      wx.showToast({
        title: '请填写联系人姓名',
        icon: "none"
      })
      return
    }
    if (!vm.data.concatphone) {
      wx.showToast({
        title: '请填写联系人电话',
        icon: "none"
      })
      return
    }
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(vm.data.concatphone)) {
      wx.showToast({
        title: '联系人电话格式不正确',
        icon: "none"
      })
      return;
    }
    AJAX("order/create-order", 'POST', {
      product_id: vm.data.productId,
      num: vm.data.num,
      customer: JSON.stringify(vm.data.studentList),
      concatname: vm.data.concatname,
      concatphone: vm.data.concatphone
    }).then(res => {
      wx.navigateTo({
        url: '/pages/order/orderinfo/orderinfo?sn=' + res.data.order_sn,
      })
    })
  },
  inputInfo(e) {
    let _type = e.currentTarget.dataset.type
    this.setData({
      [_type]: e.detail.value
    })
  },
  editTap(e) {
    let _index = e.currentTarget.dataset.index
    let _data = this.data.studentList[_index]
    let _obj = {
      index: _index,
      list: _data
    }
    wx.setStorageSync("studentedit", JSON.stringify(_obj))
    wx.navigateTo({
      url: '/pages/order/studentinfo/studentinfo',
    })
  },
  deleteTap(e) {
    let _index = e.currentTarget.dataset.index
    let _data = this.data.studentList
    _data.splice(_index, 1)
    this.setData({
      studentList: _data
    })
  },
  toRegulation() {
    wx.navigateTo({
      url: '/pages/order/regulation/regulation',
    })
  }
})